//
//  YWNoQrcodeEquipmentMaintainRecordDetailViewController.m
//  maintain
//
//  Created by 烟雨云渊 on 16/12/14.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWNoQrcodeEquipmentMaintainRecordDetailViewController.h"
#import "YWEquipmentMoreDetailTableViewController.h"
#import "YWMatchMissionsViewController.h"
#import "YWCreateTempMissionTableViewController.h"
#import "YWPostTroubleTableViewController.h"
#import "YWEquipmentTroubleMoreTableViewController.h"
#import "YWInspectionEquipmentRecordMoreTableViewController.h"
#import "YWEquipmentTroubleRecordMoreTableViewController.h"
#import "YWEquipmentRepairAndUnkeepMoreTableViewController.h"
#import "YWTroubleRecordsDetailTableViewController.h"
#import "YWMaintenanceRecordDetailTableViewController.h"
#import "YWInspecEquipmentRecordDetailTableViewController.h"
#import "YWCirculationDetailViewController.h"
#import "YWTemTroubleDetailTableViewController.h"
#import "YWEquUseMethodAndRangeTableViewController.h"

#import "YWMacro.h"
#import "UIColor+CustomColors.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "MBProgressHUD.h"
#import "UIView+Extension.h"
#import "YWConst.h"
#import "YWCommonTool.h"
#import "UIImageView+WebCache.h"
#import "BHBPopView.h"
#import "YYPhotoGroupView.h"
#import "YWCustomButton.h"
#import "MJRefresh.h"
#import "WMHttpTool.h"
#import "MJExtension.h"
#import "YWPatrolPicture.h"
#import "UIScrollView+EmptyDataSet.h"
#import "NSDate+YYAdd.h"

#import "YWEquipment.h"
#import "YWPicture.h"

#import "YWMission.h"
#import "YWInspectionEquipmentRecord.h"
#import "YWEquipmentMaintenanceRecord.h"
#import "YWMissionManager.h"
#import "YWEquipmentTroubleRecord.h"
#import "YWInspectionMissionManager.h"
#import "YWBuildingManager.h"
#import "YWBuilding.h"
#import "YWPatrolPointRecord.h"
#import "YWKeepWatchMissionManager.h"
#import "YWEquipmentFieldRecord.h"
#import "YWNoQrcodeRights.h"
#import "YWLoginTool.h"
#import "YWFunctionManager.h"
#import "YWFunctionModel.h"
#import "YWMeterReadingManager.h"
#import "YWEquipmentManager.h"
#import "YWReportTroubleManager.h"
#import "YWReportTroubleModel.h"
#import "YWTroubleMissionModel.h"

//#import "YWMyPostTroubleCell.h"
#import "YWLedgerPostTroubleTableViewCell.h"
#import "YWNoQrcodeMaintainRecordTableViewCell.h"
#import "YWEmptyTableViewCell.h"
#import "YWDetectAndUpkeepManager.h"
#import "YWTroubleMissionManager.h"

@interface YWNoQrcodeEquipmentMaintainRecordDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray * dataSource;
@property (nonatomic, strong) NSMutableArray * equipmentDataSource;
@property (nonatomic, strong) NSMutableArray * titleDataSource;

@property (nonatomic, strong) UIView * headView;
@property (nonatomic, strong) UIImageView* equipmentView;
@property (nonatomic, strong) YWCustomButton * pCountBtn;
@property (nonatomic, strong) UIView * cellView;
@property (nonatomic, strong) UILabel * equipmentLabel;
@property (nonatomic, strong) UILabel * detailLabel;
@property (nonatomic, strong) UIImageView * moreImageView;
@property (nonatomic, strong) UIView * useMethodAndRangeCell;
@property (nonatomic, strong) UILabel *useMethodAndRangeLabel;
@property (nonatomic, strong) UIImageView * useMoreImageView;
@property (nonatomic, strong) UIView * segmentView;
@property (nonatomic, strong) UILabel * maintainLabel;

@property (nonatomic, strong) UIView * footView;
@property (nonatomic, strong) UISegmentedControl *segmentedControl;
//+号按钮
//@property (nonatomic, strong) UIButton * addButton;

@property (nonatomic, strong) UILabel * seeMoreLabel;
@end

@implementation YWNoQrcodeEquipmentMaintainRecordDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = _equipment.equipmentName;
    self.view.backgroundColor = [UIColor customBackgroudColor];
    self.navigationController.view.backgroundColor = [UIColor customBackgroudColor];
    [self setupTableView];
    [self setupData];
}

- (void)setupTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -64, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[YWLedgerPostTroubleTableViewCell class] forCellReuseIdentifier:YWLedgerPostTroubleTableViewCellIdentifier];
    [_tableView registerClass:[YWNoQrcodeMaintainRecordTableViewCell class] forCellReuseIdentifier:YWNoQrcodeMaintainRecordTableViewCellIdentifier];
    [_tableView registerClass:[YWEmptyTableViewCell class] forCellReuseIdentifier:YWEmptyTableViewCellIdentifier];
    
    _tableView.backgroundColor = [UIColor customBackgroudColor];
    _tableView.tableHeaderView = self.headView;
    _tableView.tableFooterView = self.footView;
    _tableView.tableFooterView.hidden = YES;
    _tableView.estimatedRowHeight = 44.0f;
    _tableView.rowHeight = UITableViewAutomaticDimension;
    
//    [self.navigationController.view addSubview:self.addButton];
}

- (void)setupData{
    //
    [self refreshTop];
    //
    [self loadDataWithSegement:0];
}

- (void)refreshTop
{
    [self.tableView.mj_footer resetNoMoreData];
    [self requestData:YWTableViewFreshTypeDown];
}

- (void)requestData:(YWTableViewFreshType)refreshType
{
    dispatch_group_t group = dispatch_group_create();
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        //巡查
        dispatch_group_enter(group);
        [[YWInspectionMissionManager shareManager] requestInspectionEquipmentRecordWithEquipmentIds:self.equipment.equipmentId page:1 pageSize:10 completeBlock:^(NSInteger count) {
            dispatch_group_leave(group);
        }];
        //检测
        dispatch_group_enter(group);
        [YWDetectAndUpkeepManager requestEquipmentDetectAndUpkeepRecordWithMaintenanceTypeId:YWMaintenanceTypeDetection equipmentIds:self.equipment.equipmentId page:1 pageSize:10 completeBlock:^(NSInteger count) {
            dispatch_group_leave(group);
        }];
        //保养
        dispatch_group_enter(group);
        [YWDetectAndUpkeepManager requestEquipmentDetectAndUpkeepRecordWithMaintenanceTypeId:YWMaintenanceTypeUpkeep equipmentIds:self.equipment.equipmentId page:1 pageSize:10 completeBlock:^(NSInteger count) {
            dispatch_group_leave(group);
        }];
        //维修记录
        dispatch_group_enter(group);
        [[YWTroubleMissionManager shareManager] requestTroubleRecordWithEquipmentIds:self.equipment.equipmentId page:1 pageSize:10 completeBlock:^(NSInteger count) {
            dispatch_group_leave(group);
        }];
        //报障
        dispatch_group_enter(group);
        [[YWReportTroubleManager shareManager] requestPostTroubleWithEquipmentId:self.equipment.equipmentId page:1 pageSize:10 completeBlock:^(BOOL success) {
            dispatch_group_leave(group);
        }];
        
        YWWeakSelf(weakSelf);
        dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
        dispatch_async(dispatch_get_main_queue(), ^{
            if (refreshType == YWTableViewFreshTypeDown) {
                [weakSelf.tableView.mj_header endRefreshing];
            }
            else {
                [weakSelf.tableView.mj_footer endRefreshing];
            }
            [weakSelf.dataSource removeAllObjects];
            [self loadDataWithSegement:_segmentedControl.selectedSegmentIndex];
            [weakSelf.tableView reloadData];
        });
    });
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count>0?self.dataSource.count:1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count>0?[self.dataSource[section] count]:1;;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        if ([self.dataSource count]>0) {
        id object = self.dataSource[indexPath.section][indexPath.row];
        if ([object isKindOfClass:[YWReportTroubleModel class]]) {
            //报障
            YWLedgerPostTroubleTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:YWLedgerPostTroubleTableViewCellIdentifier];
            cell.reportTroulbe = (YWReportTroubleModel*)object;
            return cell;
        }else if ([object isKindOfClass:[YWInspectionEquipmentRecord class]]){
            //巡查
            YWNoQrcodeMaintainRecordTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:YWNoQrcodeMaintainRecordTableViewCellIdentifier];
            cell.inspectionRecord = (YWInspectionEquipmentRecord*)object;
            return cell;
        }else if([object isKindOfClass:[YWEquipmentTroubleRecord class]]){
            //维修
            YWNoQrcodeMaintainRecordTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:YWNoQrcodeMaintainRecordTableViewCellIdentifier];
            cell.troubleRecord = (YWEquipmentTroubleRecord*)object;
            return cell;
        }else if ([object isKindOfClass:[YWEquipmentMaintenanceRecord class]]){
            YWNoQrcodeMaintainRecordTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:YWNoQrcodeMaintainRecordTableViewCellIdentifier];
            cell.maintenceRecord = (YWEquipmentMaintenanceRecord*)object;
            return cell;
        }else{
            YWNoQrcodeMaintainRecordTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:YWNoQrcodeMaintainRecordTableViewCellIdentifier];
            cell.maintenceRecord = (YWEquipmentMaintenanceRecord*)object;
            return cell;
        }
    }else{
        YWEmptyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:YWEmptyTableViewCellIdentifier];
        cell.backgroundColor = [UIColor customBackgroudColor];
        return cell;
    }
}

//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    if ([self.dataSource count]>0) {
//        id object = self.dataSource[indexPath.section][indexPath.row];
//        if ([object isKindOfClass:[YWReportTroubleModel class]]) {
//            return [tableView fd_heightForCellWithIdentifier:YWLedgerPostTroubleTableViewCellIdentifier cacheByIndexPath:indexPath configuration:^(YWLedgerPostTroubleTableViewCell *cell) {
//                cell.reportTroulbe = (YWReportTroubleModel*)object;
//            }];
//        }else{
//            return [tableView fd_heightForCellWithIdentifier:YWNoQrcodeMaintainRecordTableViewCellIdentifier cacheByIndexPath:indexPath configuration:^(YWNoQrcodeMaintainRecordTableViewCell *cell) {
//                if ([object isKindOfClass:[YWInspectionEquipmentRecord class]]) {
//                    cell.inspectionRecord = (YWInspectionEquipmentRecord*)object;
//                    //巡查
//                }else if ([object isKindOfClass:[YWEquipmentTroubleRecord class]]){
//                    //维修
//                    cell.troubleRecord = (YWEquipmentTroubleRecord*)object;
//                }else{
//                    //检测、保养
//                    cell.maintenceRecord = (YWEquipmentMaintenanceRecord*)object;
//                }
//            }];
//        }
//    }else{
//        return 150;
//    }
//}

#pragma mark - table view section header view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if ([self.dataSource count]>0) {
        static NSString* HeaderIdentifier = @"YWNoQrcodeEquipmentMaintainRecordDetailViewControllerForHeaderInSection";
        UITableViewHeaderFooterView* headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];
        if (headerView == nil) {
            headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:HeaderIdentifier];
            headerView.contentView.backgroundColor = [UIColor customBackgroudColor];
            UILabel* label = [[UILabel alloc] init];
            label.textAlignment = NSTextAlignmentLeft;
            label.textColor = [UIColor blackColor];
            label.tag = 2016;
            label.font = KTitleFont;
            [headerView addSubview:label];
        }
        
        NSString *headText = [self titleForHeaderInSection:section];
        UILabel* label = [headerView viewWithTag:2016];
        label.text = headText;
        label.frame = CGRectMake(15, 0, SCREEN_WIDTH - 2 * 15, 35);
        return headerView;
    }else{
        return nil;
    }
}

- (NSString *)titleForHeaderInSection:(NSInteger)section{
    NSArray *array = [self.dataSource[section] count]>0?self.dataSource[section]:nil;
    id object = array.count?array[0]:nil;
    if ([object isKindOfClass:[YWReportTroubleModel class]]) {
        YWReportTroubleModel * reportTrouble = array.count?array[0]:nil;
        NSString *time = [YWCommonTool showChineseFormatterDate:reportTrouble.reportTime/1000];
        NSInteger weekDay = [NSDate dateWithTimeIntervalSince1970:reportTrouble.reportTime/1000].weekday;
        NSString *weekStr = [YWCommonTool weekDayString:weekDay-1];
        NSString * title = [NSString stringWithFormat:@"%@  %@",time,weekStr];
        return title;
    }else if ([object isKindOfClass:[YWInspectionEquipmentRecord class]]){
        YWInspectionEquipmentRecord *equipmentRecord = array.count?array[0]:nil;
        NSString *time = [YWCommonTool showChineseFormatterDate:equipmentRecord.executeTime/1000];
        NSInteger weekDay = [NSDate dateWithTimeIntervalSince1970:equipmentRecord.executeTime/1000].weekday;
        NSString *weekStr = [YWCommonTool weekDayString:weekDay-1];
        NSString * title = [NSString stringWithFormat:@"%@  %@",time,weekStr];
        return title;
    }else if([object isKindOfClass:[YWEquipmentTroubleRecord class]]){
        YWEquipmentTroubleRecord *troubleRecord = array.count?array[0]:nil;
        NSString *time = [YWCommonTool showChineseFormatterDate:troubleRecord.completeTime/1000];
        NSInteger weekDay = [NSDate dateWithTimeIntervalSince1970:troubleRecord.completeTime/1000].weekday;
        NSString *weekStr = [YWCommonTool weekDayString:weekDay-1];
        NSString * title = [NSString stringWithFormat:@"%@  %@",time,weekStr];
        return title;
    }else{
        YWEquipmentMaintenanceRecord * maintanceRecord = array.count?array[0]:nil;
        NSString *time = [YWCommonTool showChineseFormatterDate:maintanceRecord.completeTime/1000];
        NSInteger weekDay = [NSDate dateWithTimeIntervalSince1970:maintanceRecord.completeTime/1000].weekday;
        NSString *weekStr = [YWCommonTool weekDayString:weekDay-1];
        NSString * title = [NSString stringWithFormat:@"%@  %@",time,weekStr];
        return title;
    }
}

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{
    if ([self.dataSource count]>0) {
        return 35;
    }else{
        return 0.01;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.dataSource.count>0) {
        id object = self.dataSource[indexPath.section][indexPath.row];
        if ([object isKindOfClass:[YWEquipmentTroubleRecord class]]) {
            YWTroubleRecordsDetailTableViewController* troubleDetailVC = [[YWTroubleRecordsDetailTableViewController alloc] init];
            //troubleDetailVC.locationId = self.equipment.locationId;
            troubleDetailVC.troubleRecord = (YWEquipmentTroubleRecord*)object;
            [self.navigationController pushViewController:troubleDetailVC animated:YES];
        }
        else if ([object isKindOfClass:[YWEquipmentMaintenanceRecord class]]) {
            YWMaintenanceRecordDetailTableViewController* maintenanceVC = [[YWMaintenanceRecordDetailTableViewController alloc] init];
            maintenanceVC.maintenanceRecord = (YWEquipmentMaintenanceRecord*)object;
            [self.navigationController pushViewController:maintenanceVC animated:YES];
        }
        else if ([object isKindOfClass:[YWInspectionEquipmentRecord class]]) {
            YWInspecEquipmentRecordDetailTableViewController* equipmentDetailRecord = [[YWInspecEquipmentRecordDetailTableViewController alloc] init];
            equipmentDetailRecord.inspectionEquipmentRecord = (YWInspectionEquipmentRecord*)object;
            [self.navigationController pushViewController:equipmentDetailRecord animated:YES];
        }else if ([object isKindOfClass:[YWReportTroubleModel class]]){
            YWReportTroubleModel *reportTrouble = (YWReportTroubleModel*)object;
            if (reportTrouble.troubleId<=0 && reportTrouble.troubleStatus == YWTroubleMissionStatusFinish) {
                YWTemTroubleDetailTableViewController *tempTroubleVC = [[YWTemTroubleDetailTableViewController alloc] init];
                tempTroubleVC.reportTrouble = reportTrouble;
                [self.navigationController pushViewController:tempTroubleVC animated:YES];
            }else{
                YWCirculationDetailViewController *circulationDetailVC = [[YWCirculationDetailViewController alloc] init];
                circulationDetailVC.isNotShowHandleButton = YES;
                circulationDetailVC.troubleMissionModel = reportTrouble.troubleMission;
                [self.navigationController pushViewController:circulationDetailVC animated:YES];
            }
        }
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //当前偏移量
    CGFloat offset = scrollView.contentOffset.y;
    CGFloat alpah = offset/70;
    [self.navigationController.navigationBar setValue:@(alpah) forKeyPath:@"backgroundView.alpha"];
}

#pragma mark -----懒加载
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (NSMutableArray*)equipmentDataSource
{
    if (!_equipmentDataSource) {
        _equipmentDataSource = [NSMutableArray array];
        
        NSMutableArray *buliding = [NSMutableArray array];
        //        楼盘名
        [buliding addObject:[[YWBuildingManager shareManager]buildingWithId:self.equipment.buildingId].buildingName?:@"未填写"];
        //        几期几层几号
        [buliding addObject:[[YWBuildingManager shareManager] equipmentLocationNobuilding:self.equipment]?:@"未填写"];
        [_equipmentDataSource addObject:buliding];
        
        
        NSInteger eEquipmentTypeId = [[YWMeterReadingManager shareManager] electricMeterEquipmentTypeId];
        NSInteger wEquipmentTypeId = [[YWMeterReadingManager shareManager] waterMeterEquipmentTypeId];
        NSInteger gEquipmentTypeId = [[YWMeterReadingManager shareManager] gasMeterEquipmentTypeId];
        
        if (self.equipment.equipmentTypeId == eEquipmentTypeId || self.equipment.equipmentTypeId == wEquipmentTypeId || self.equipment.equipmentTypeId == gEquipmentTypeId) {
            NSMutableArray *equipment = [NSMutableArray array];
            //        设备名称 设备名在titleDataSource中
            [equipment addObject:@""];
            //      单位名称
            [equipment addObject:self.equipment.ownCode.length?self.equipment.ownCode:@"未填写"];
            YWMeterProperty *meterProperty = [[YWEquipmentManager shareManager] meterPropertyWithEquipmentId:self.equipment.equipmentId];
            NSString * companyName = [NSString stringWithFormat:@"%@",meterProperty.companyName.length>0 ? meterProperty.companyName:@"无"];
            [equipment addObject:companyName];
            //      房号
            NSString * place = [NSString stringWithFormat:@"%@",meterProperty.place.length>0 ? meterProperty.place:@"无"];
            [equipment addObject:place];
            //      倍率
            NSString * rate = [NSString stringWithFormat:@"%.1f",meterProperty.rate>0?meterProperty.rate:1.0];
            [equipment addObject:rate];
            //        出厂编号
            [equipment addObject:self.equipment.factoryCode.length?self.equipment.factoryCode: @"未填写"];
            //        设备型号
            [equipment addObject:self.equipment.model.length?self.equipment.model:@"未填写"];
            //        设备厂商
            [equipment addObject:self.equipment.factoryName.length?self.equipment.factoryName:@"未填写"];
            //        使用年限
            [equipment addObject:self.equipment.durableYears>0?[NSString stringWithFormat:@"%ld年",(long)self.equipment.durableYears]:@"未填写"];
            //初始参数
            [equipment addObject:@""];
            //使用方法和范围
            [equipment addObject:@""];
            [_equipmentDataSource addObject:equipment];
        }else{
            NSMutableArray *equipment = [NSMutableArray array];
            //        设备名称 设备名在titleDataSource中
            [equipment addObject:@""];
            //        自编码
            [equipment addObject:self.equipment.ownCode.length?self.equipment.ownCode:@"未填写"];
            //        出厂编号
            [equipment addObject:self.equipment.factoryCode.length?self.equipment.factoryCode: @"未填写"];
            //        设备型号
            [equipment addObject:self.equipment.model.length?self.equipment.model:@"未填写"];
            //        设备厂商
            [equipment addObject:self.equipment.factoryName.length?self.equipment.factoryName:@"未填写"];
            //        使用年限
            [equipment addObject:self.equipment.durableYears>0?[NSString stringWithFormat:@"%ld年",(long)self.equipment.durableYears]:@"未填写"];
            //初始参数
            [equipment addObject:@""];
            //使用方法和范围
            [equipment addObject:@""];
            [_equipmentDataSource addObject:equipment];
        }
        NSMutableArray *date = [NSMutableArray array];
        //        生产日期
        [date addObject:self.equipment.produceDate>0?[YWCommonTool showChineseFormatterDate:self.equipment.produceDate / 1000]:@"未填写"];
        //        采购日期
        [date addObject:self.equipment.buyDate>0?[YWCommonTool showChineseFormatterDate:self.equipment.buyDate/1000]:@"未填写"];
        //        安装日期
        [date addObject:self.equipment.installDate>0?[YWCommonTool showChineseFormatterDate:self.equipment.installDate / 1000]:@"未填写"];
        
        [_equipmentDataSource addObject:date];
    }
    return _equipmentDataSource;
}

- (NSMutableArray*)titleDataSource{
    if (!_titleDataSource) {
        _titleDataSource = [NSMutableArray array];
        NSInteger eEquipmentTypeId = [[YWMeterReadingManager shareManager] electricMeterEquipmentTypeId];
        NSInteger wEquipmentTypeId = [[YWMeterReadingManager shareManager] waterMeterEquipmentTypeId];
        NSInteger gEquipmentTypeId = [[YWMeterReadingManager shareManager] gasMeterEquipmentTypeId];
        
        if (self.equipment.equipmentTypeId == eEquipmentTypeId || self.equipment.equipmentTypeId == wEquipmentTypeId || self.equipment.equipmentTypeId == gEquipmentTypeId) {
            [_titleDataSource addObjectsFromArray:@[@[@""],@[self.equipment.equipmentName?:@"",@"用户自编码",@"单位名称",@"房号",@"倍率",@"出厂编号",@"设备型号",@"设备厂商",@"使用年限",@"初始参数",@"使用方法和范围"],@[@"生产日期",@"采购日期",@"安装日期"]]];
        }else{
            [_titleDataSource addObjectsFromArray:@[@[@""],@[self.equipment.equipmentName?:@"",@"用户自编码",@"出厂编号",@"设备型号",@"设备厂商",@"使用年限",@"初始参数",@"使用方法和范围"],@[@"生产日期",@"采购日期",@"安装日期"]]];
        }
    }
    return _titleDataSource;
}

- (UIView*)headView{
    if (!_headView) {
        UIView * headView = [[UIView alloc] init];
        headView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 370);
        
        CGFloat pictureViewHeight = 160;
        CGFloat pCountBtnH = 50;
        //首部图片
        YWPicture *picture = _equipment.pictures.count>0?_equipment.pictures[0]:nil;
        NSString* downloadUrl = [YWCommonTool pictureDownloadUrl:picture.smallPictureUrl];
        [self.equipmentView sd_setImageWithURL:[NSURL URLWithString:downloadUrl] placeholderImage:[UIImage imageNamed:@"image_placeholder"]];
        self.equipmentView.frame = CGRectMake(0, 0, SCREEN_WIDTH, pictureViewHeight);
        [headView addSubview:self.equipmentView];
        
        //右下角图片张数,边沿10，H：60 W：60
        [self.pCountBtn setFrame: CGRectMake(SCREEN_WIDTH-kGapH-pCountBtnH, self.equipmentView.frame.size.height-kGapH-pCountBtnH, pCountBtnH, pCountBtnH)];
        [self.equipmentView addSubview:self.pCountBtn];
        
        //cellView
        CGFloat cellViewHeight = 60;
        CGFloat equipmentLabelH = 30;
        CGFloat detailLabelH = 20;
        CGFloat moreImageViewW = 40;
        
        [self.cellView setFrame:CGRectMake(0, pictureViewHeight, SCREEN_WIDTH, cellViewHeight)];
        [self.equipmentLabel  setFrame:CGRectMake(kGapH, 5, SCREEN_WIDTH-kGapH*2, equipmentLabelH)];
        [self.cellView addSubview:self.equipmentLabel];
        
        [self.detailLabel setFrame:CGRectMake(kGapH, equipmentLabelH, SCREEN_WIDTH-kGapH*2-moreImageViewW, detailLabelH)];
        [self.cellView addSubview:self.detailLabel];
        [self.moreImageView setFrame:CGRectMake(SCREEN_WIDTH-moreImageViewW-kGapH, equipmentLabelH, moreImageViewW, detailLabelH)];
        [self.cellView addSubview:self.moreImageView];
        [headView addSubview:self.cellView];
        
        //使用范围和方法
        [self.useMethodAndRangeCell setFrame:CGRectMake(0, CGRectGetMaxY(self.cellView.frame)+1, SCREEN_WIDTH, 40)];
        
        [self.useMethodAndRangeLabel setFrame:CGRectMake(kGapH, kGapH, CGRectGetWidth(self.detailLabel.frame), detailLabelH)];
        [self.useMoreImageView setFrame:CGRectMake(SCREEN_WIDTH-moreImageViewW-kGapH, kGapH, moreImageViewW, detailLabelH)];
        [self.useMethodAndRangeCell addSubview:self.useMethodAndRangeLabel];
        [self.useMethodAndRangeCell addSubview:self.useMoreImageView];
        [headView addSubview:self.useMethodAndRangeCell];
        
        //选择栏
        CGFloat segmentViewHeight = 96.0;
        CGFloat maintainLabelWidth = 80;
        
        [self.segmentView setFrame:CGRectMake(0,CGRectGetMaxY(self.useMethodAndRangeCell.frame), SCREEN_WIDTH, segmentViewHeight)];
        [self.maintainLabel setFrame:CGRectMake(kGapH, kGapH, maintainLabelWidth, kLabelH)];
        [self.segmentView addSubview:self.maintainLabel];
        
        [self.segmentedControl setFrame:CGRectMake(kGapH, kGapH*2+kLabelH, SCREEN_WIDTH-kGapH*2, KbottomViewH)];
        [self.segmentView addSubview:self.segmentedControl];
        [headView addSubview:self.segmentView];
        _headView = headView;
    }
    return _headView;
}

- (UIImageView*)equipmentView{
    if (!_equipmentView) {
        UIImageView* equipmentView = [[UIImageView alloc] init];
        equipmentView.contentMode = UIViewContentModeScaleAspectFill;
        equipmentView.clipsToBounds = YES;
        equipmentView.userInteractionEnabled = YES;
        _equipmentView = equipmentView;
    }
    return _equipmentView;
}

- (YWCustomButton*)pCountBtn{
    if (!_pCountBtn) {
        YWCustomButton * pCountBtn = [[YWCustomButton alloc] init];
        pCountBtn.backgroundColor = [UIColor clearColor];
        pCountBtn.clipsToBounds = YES;
        pCountBtn.layer.cornerRadius = 25.0;
        pCountBtn.layer.borderWidth = 1.0;
        pCountBtn.layer.borderColor = [UIColor whiteColor].CGColor;
        [pCountBtn setImage:[UIImage imageNamed:@"profile_picture"] forState:UIControlStateNormal];
        [pCountBtn setTitle:[NSString stringWithFormat:@"%lu张",(unsigned long)_equipment.pictures.count] forState:UIControlStateNormal];
        [pCountBtn addTarget:self action:@selector(pCountBtnTouchEvent:) forControlEvents:UIControlEventTouchUpInside];
        _pCountBtn = pCountBtn;
    }
    return _pCountBtn;
}

- (UIView*)cellView{
    if (!_cellView) {
        UIView * cellView = [[UIView alloc] init];
        cellView.backgroundColor = [UIColor whiteColor];
        UITapGestureRecognizer * handleGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellViewGestureRecognizer:)];
        [cellView addGestureRecognizer:handleGesture];
        _cellView = cellView;
    }
    return _cellView;
}

- (UILabel*)equipmentLabel{
    if (!_equipmentLabel) {
        UILabel * equipmentLabel = [[UILabel alloc] init];
        equipmentLabel.text = _equipment.equipmentName;
        equipmentLabel.textAlignment = NSTextAlignmentLeft;
        equipmentLabel.font = KTitleFont;
        _equipmentLabel= equipmentLabel;
    }
    return _equipmentLabel;
}

- (UILabel*)detailLabel{
    if (!_detailLabel) {
        UILabel * detailLabel = [[UILabel alloc] init];
        detailLabel.textAlignment = NSTextAlignmentLeft;
        detailLabel.text = _equipment.ownCode;
        detailLabel.font = [UIFont systemFontOfSize:14];
        _detailLabel = detailLabel;
    }
    return _detailLabel;
}

- (UIImageView*)moreImageView{
    if (!_moreImageView) {
        UIImageView * moreImageView = [[UIImageView alloc] init];
        moreImageView.image = [UIImage imageNamed:@"profile_seemore"];
        _moreImageView = moreImageView;
    }
    return _moreImageView;
}

- (UIView*)useMethodAndRangeCell{
    if (!_useMethodAndRangeCell) {
        UIView * useMethodAndRangeCell = [[UIView alloc] init];
        useMethodAndRangeCell.backgroundColor = [UIColor whiteColor];
        UITapGestureRecognizer * useMethodAndRangeGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(useMethodAndRangeCellGestureRecognizer:)];
        [useMethodAndRangeCell addGestureRecognizer:useMethodAndRangeGesture];
        _useMethodAndRangeCell = useMethodAndRangeCell;
    }
    return _useMethodAndRangeCell;
}

- (UILabel*)useMethodAndRangeLabel{
    if (!_useMethodAndRangeLabel) {
        UILabel *useMethodAndRangeLabel = [[UILabel alloc] init];
        useMethodAndRangeLabel.text = @"使用范围和方法";
        useMethodAndRangeLabel.textAlignment = NSTextAlignmentLeft;
        _useMethodAndRangeLabel = useMethodAndRangeLabel;
    }
    return _useMethodAndRangeLabel;
}

- (UIImageView*)useMoreImageView{
    if (!_useMoreImageView) {
        UIImageView * useMoreImageView = [[UIImageView alloc] init];
        useMoreImageView.image = [UIImage imageNamed:@"profile_seemore"];
        _useMoreImageView = useMoreImageView;
    }
    return _useMoreImageView;
}

- (UIView*)segmentView{
    if (!_segmentView) {
        UIView * segmentView = [[UIView alloc] init];
        segmentView.backgroundColor = [UIColor customBackgroudColor];
        _segmentView = segmentView;
    }
    return _segmentView;
}

- (UILabel*)maintainLabel{
    if (!_maintainLabel) {
        UILabel * maintainLabel = [[UILabel alloc] init];
        maintainLabel.text = @"维保记录";
        maintainLabel.textAlignment = NSTextAlignmentLeft;
        _maintainLabel = maintainLabel;
    }
    return _maintainLabel;
}

- (UISegmentedControl*)segmentedControl{
    if (!_segmentedControl) {
        NSArray * items = @[@"报障",@"维修",@"巡查",@"检测",@"保养"];
        UISegmentedControl *segmentControl=[[UISegmentedControl alloc]initWithItems:items];
        segmentControl.selectedSegmentIndex = 0;
        segmentControl.apportionsSegmentWidthsByContent = YES;//宽度自适应
        segmentControl.tintColor = [UIColor customGreenColor];
        [segmentControl addTarget:self action:@selector(segmentControlEvent:) forControlEvents:UIControlEventValueChanged];
        _segmentedControl = segmentControl;
    }
    return _segmentedControl;
}

- (UIView*)footView{
    if (!_footView) {
        UIView * footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
        self.seeMoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(kGapH, 5, SCREEN_WIDTH-kGapH*2, 30)];
        self.seeMoreLabel.text = @"查看更多报障记录";
        self.seeMoreLabel.textColor = [UIColor customTextGrayColor];
        self.seeMoreLabel.textAlignment = NSTextAlignmentCenter;
        [footView addSubview:self.seeMoreLabel];
        UITapGestureRecognizer * handleGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(footViewGestureRecognizer:)];
        [footView addGestureRecognizer:handleGesture];
        _footView = footView;
    }
    return _footView;
}
/*
- (UIButton*)addButton{
    if (!_addButton) {
        UIButton * addButton = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat buttonSize = 50;
        [addButton setImage:[UIImage imageNamed:@"common_ledger_add"] forState:UIControlStateNormal];
        addButton.layer.masksToBounds = YES;
        addButton.frame = CGRectMake(SCREEN_WIDTH-kMarginW-buttonSize, SCREEN_HEIGHT-kMarginW-buttonSize, buttonSize, buttonSize);        
        [addButton addTarget:self action:@selector(addButtonTouchEvent:) forControlEvents:UIControlEventTouchUpInside];
        _addButton = addButton;
    }
    return _addButton;
}
*/
#pragma mark ----标签栏的响应事件
- (void)segmentControlEvent:(UISegmentedControl *)segmentControl{
    [self loadDataWithSegement:segmentControl.selectedSegmentIndex];
}

- (void)loadDataWithSegement:(NSInteger)segement{
    //切换数据源用
    YWLog(@"segmentControl %ld",(long)segement);
    if (segement == 0) {
        [self.dataSource removeAllObjects];
        //报障
        NSArray * troublesArray = [[YWReportTroubleManager shareManager] getReportTroubleWithEquipmentId:_equipment.equipmentId page:1];
        [self.dataSource addObjectsFromArray:troublesArray];
        self.seeMoreLabel.text = @"查看更多报障记录";
    }else if (segement == 1){
        [self.dataSource removeAllObjects];
        //维修
        NSArray * troubleRecordArray = [[YWTroubleMissionManager shareManager] troubleRecordArrayWithEquipmentId:_equipment.equipmentId andPage:1];
        [self.dataSource addObjectsFromArray:troubleRecordArray];
        self.seeMoreLabel.text = @"查看更多维修记录";
    }else if (segement == 2){
        [self.dataSource removeAllObjects];
        //巡查
        NSArray * patrolArray = [[YWMissionManager shareManager] patrolRecordsWithEquipmentId:_equipment.equipmentId andPage:1];
        [self.dataSource addObjectsFromArray:patrolArray];
        self.seeMoreLabel.text = @"查看更多巡查记录";
    }else if (segement == 3){
        [self.dataSource removeAllObjects];
        NSArray * detectionArray = [YWDetectAndUpkeepManager maintenanceRecordArrayWithEquipmentId:_equipment.equipmentId maintenanceTypeId:YWMaintenanceTypeDetection withPage:1];
        [self.dataSource addObjectsFromArray:detectionArray];
        self.seeMoreLabel.text = @"查看更多检测记录";
    }else{
        [self.dataSource removeAllObjects];
        NSArray * upkeepArray =
        [YWDetectAndUpkeepManager maintenanceRecordArrayWithEquipmentId:_equipment.equipmentId maintenanceTypeId:YWMaintenanceTypeUpkeep withPage:1];
        [self.dataSource addObjectsFromArray:upkeepArray];
        self.seeMoreLabel.text = @"查看更多保养记录";
    }
    NSMutableArray * countArray = [NSMutableArray array];
    [self.dataSource enumerateObjectsUsingBlock:^(NSArray *  _Nonnull array, NSUInteger idx, BOOL * _Nonnull stop) {
        [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [countArray addObject:obj];
        }];
    }];
    if (countArray.count>9) {
        self.tableView.tableFooterView.hidden = NO;
    }else{
        self.tableView.tableFooterView.hidden = YES;
    }
    [self.tableView reloadData];
}

 - (void)pCountBtnTouchEvent:(UIButton*)sender{
     NSMutableArray *items = [NSMutableArray new];
     for (NSUInteger i = 0, max = _equipment.pictures.count; i < max; i++) {
         YYPhotoGroupItem *item = [YYPhotoGroupItem new];
         YWPicture *picture = _equipment.pictures[i];
         UIImageView * imageView = [[UIImageView alloc] init];
         NSString* downloadUrl = [YWCommonTool pictureDownloadUrl:picture.smallPictureUrl];
         [imageView sd_setImageWithURL:[NSURL URLWithString:downloadUrl]];
         item.thumbView = imageView;
         item.largeImageURL = [NSURL URLWithString:[YWCommonTool pictureDownloadUrl:picture.bigPictureUrl]];;
         [items addObject:item];
     }
     YYPhotoGroupView *v = [[YYPhotoGroupView alloc] initWithGroupItems:items];
     [v presentFromImageView:self.equipmentView toContainer:[UIApplication sharedApplication].keyWindow animated:YES completion:nil];
 }
/*
- (void)addButtonTouchEvent:(UIButton*)sender{
    BHBItem * inspectionItem = [[BHBItem alloc]initWithTitle:@"巡查" Icon:@"profile_patrol"];
    BHBItem * keepWatchItem = [[BHBItem alloc]initWithTitle:@"检测" Icon:@"profile_detection"];
    BHBItem * myTroublesItem = [[BHBItem alloc]initWithTitle:@"保养" Icon:@"profile_upkeep"];
    BHBItem * maintenanceRecordItem = [[BHBItem alloc]initWithTitle:@"报障" Icon:@"profile_postTrouble"];
    BHBItem * checkItem = [[BHBItem alloc]initWithTitle:@"维修" Icon:@"profile_repair"];
//    BHBItem * cancelItem = [[BHBItem alloc]initWithTitle:@"取消" Icon:@"tabbar_compose_background_icon_close"];
    //添加popview
    [BHBPopView showToView:self.view.window withItems:@[inspectionItem,keepWatchItem,myTroublesItem,maintenanceRecordItem,checkItem] andSelectBlock:^(BHBItem *item) {
        if ([item isKindOfClass:[BHBGroup class]]) {
            
        }else{
            if ([item.title isEqualToString:@"巡查"]) {
                //设备巡查
                YWFunctionModel * function = [[YWFunctionManager shareManager] getFunctionModelWithFunctionModelId:YWFunctionIdInspection];
                if (function.offOrOn == YWFunctionStatusOn) {
                    
                    NSArray *inspectionMissions = nil;
                    if (_isScanEnter) {
                        inspectionMissions = [[YWInspectionMissionManager shareManager] matchInspectionMissionsWithQrcodeID:self.equipment.qrcodeId orLocationId:self.equipment.locationId inspectionLineType:YWInspectionLineTypeEquipment equipmentId:self.equipment.equipmentId];
                    }else{
                        NSInteger value = [[[NSUserDefaults standardUserDefaults] objectForKey:KYWNoQrcodeTypeInspectionKey] integerValue];
                        if (value!=1) {
                            inspectionMissions = [[YWInspectionMissionManager shareManager] matchInspectionMissionsWithQrcodeID:self.equipment.qrcodeId orLocationId:self.equipment.locationId inspectionLineType:YWInspectionLineTypeEquipment equipmentId:self.equipment.equipmentId];
                        }
                    }
                    if (inspectionMissions.count>0) {
                        YWMatchMissionsViewController *matchMissionVC = [[YWMatchMissionsViewController alloc] init];
                        matchMissionVC.matchMissions = inspectionMissions;
                        matchMissionVC.qrCodeId = self.equipment.qrcodeId;
                        matchMissionVC.locationId = self.equipment.locationId;
                        [self.navigationController pushViewController:matchMissionVC animated:YES];
                    }else{
                        YWCreateTempMissionTableViewController *tempMissionVC = [[YWCreateTempMissionTableViewController alloc] init];
                        tempMissionVC.maintenanceType = YWMaintenanceTypePatrol;
                        tempMissionVC.qrcodeId = self.equipment.qrcodeId;
                        tempMissionVC.locationId = self.equipment.locationId;
                        tempMissionVC.currentEquipmentId = _equipment.equipmentId;
                        tempMissionVC.isLocationInspection = NO;
                        tempMissionVC.isEquipmentInspection = YES;
                        [self.navigationController pushViewController:tempMissionVC animated:YES];
                    }
                }else{
                    UIAlertController* alertController= [UIAlertController alertControllerWithTitle:@"提示" message:@"功能已关闭。如需再次使用巡查功能，您可从“设置-功能”中再次打开。" preferredStyle:UIAlertControllerStyleAlert];
                    [alertController addAction:[UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleCancel handler:nil]];
                    [self presentViewController:alertController animated:YES completion:nil];
                }
            }else if ([item.title isEqualToString:@"检测"]){
                YWFunctionModel * function = [[YWFunctionManager shareManager] getFunctionModelWithFunctionModelId:YWFunctionIdDetection];
                if (function.offOrOn == YWFunctionStatusOn) {
                    NSArray *missions = nil;
                    if (_isScanEnter) {
//                        missions = [[YWMissionManager shareManager] missionsWithQrcodeID:self.equipment.qrcodeId orLocationId:self.equipment.locationId andMaintenanceTypeId:YWMaintenanceTypeDetection];
                        missions = [[YWMissionManager shareManager] missionsWithEquipmentId:self.equipment.equipmentId andMaintenanceTypeId:YWMaintenanceTypeDetection];
                    }else{
                        NSInteger value = [[[NSUserDefaults standardUserDefaults] objectForKey:KYWNoQrcodeTypeDetectionKey] integerValue];
                        if (value!=1) {
                            missions = [[YWMissionManager shareManager] missionsWithQrcodeID:self.equipment.qrcodeId orLocationId:self.equipment.locationId andMaintenanceTypeId:YWMaintenanceTypeDetection];
                        }
                    }
                    if (missions.count>0) {
                        YWMatchMissionsViewController *matchMissionVC = [[YWMatchMissionsViewController alloc] init];
                        matchMissionVC.matchMissions = missions;
                        matchMissionVC.qrCodeId = self.equipment.qrcodeId;
                        matchMissionVC.locationId = self.equipment.locationId;
                        [self.navigationController pushViewController:matchMissionVC animated:YES];
                    }else{
                        YWCreateTempMissionTableViewController *tempMissionVC = [[YWCreateTempMissionTableViewController alloc] init];
                        tempMissionVC.qrcodeId = self.equipment.qrcodeId;
                        tempMissionVC.locationId = self.equipment.locationId;
                        tempMissionVC.currentEquipmentId = self.equipment.equipmentId;
                        tempMissionVC.maintenanceType = YWMaintenanceTypeDetection;
                        [self.navigationController pushViewController:tempMissionVC animated:YES];
                    }
                }else{
                    UIAlertController* alertController= [UIAlertController alertControllerWithTitle:@"提示" message:@"功能已关闭。如需再次使用检测功能，您可从“设置-功能”中再次打开。" preferredStyle:UIAlertControllerStyleAlert];
                    [alertController addAction:[UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleCancel handler:nil]];
                    [self presentViewController:alertController animated:YES completion:nil];
                }
            }else if ([item.title isEqualToString:@"保养"]){
                YWFunctionModel * function = [[YWFunctionManager shareManager] getFunctionModelWithFunctionModelId:YWFunctionIdUpkeep];
                if (function.offOrOn == YWFunctionStatusOn) {
                    NSArray* missions = nil;
                    if (_isScanEnter) {
//                        missions = [[YWMissionManager shareManager] missionsWithQrcodeID:self.equipment.qrcodeId orLocationId:self.equipment.locationId andMaintenanceTypeId:YWMaintenanceTypeUpkeep];
                        missions = [[YWMissionManager shareManager] missionsWithEquipmentId:self.equipment.equipmentId andMaintenanceTypeId:YWMaintenanceTypeUpkeep];
                    }else{
                        NSInteger value = [[[NSUserDefaults standardUserDefaults] objectForKey:KYWNoQrcodeTypeUpkeepKey] integerValue];
                        if (value!=1) {
                            missions = [[YWMissionManager shareManager] missionsWithQrcodeID:self.equipment.qrcodeId orLocationId:self.equipment.locationId andMaintenanceTypeId:YWMaintenanceTypeUpkeep];
                        }
                    }
                    if (missions.count>0) {
                        YWMatchMissionsViewController *matchMissionVC = [[YWMatchMissionsViewController alloc] init];
                        matchMissionVC.matchMissions = missions;
                        matchMissionVC.qrCodeId = self.equipment.qrcodeId;
                        matchMissionVC.locationId = self.equipment.locationId;
                        [self.navigationController pushViewController:matchMissionVC animated:YES];
                    }else{
                        YWCreateTempMissionTableViewController *tempMissionVC = [[YWCreateTempMissionTableViewController alloc] init];
                        tempMissionVC.qrcodeId = self.equipment.qrcodeId;
                        tempMissionVC.locationId = self.equipment.locationId;
                        tempMissionVC.currentEquipmentId = self.equipment.equipmentId;
                        tempMissionVC.maintenanceType = YWMaintenanceTypeUpkeep;
                        [self.navigationController pushViewController:tempMissionVC animated:YES];
                    }
                }else{
                    UIAlertController* alertController= [UIAlertController alertControllerWithTitle:@"提示" message:@"功能已关闭。如需再次使用保养功能，您可从“设置-功能”中再次打开。" preferredStyle:UIAlertControllerStyleAlert];
                    [alertController addAction:[UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleCancel handler:nil]];
                    [self presentViewController:alertController animated:YES completion:nil];
                }
            }else if ([item.title isEqualToString:@"报障"]){
                YWFunctionModel * function = [[YWFunctionManager shareManager] getFunctionModelWithFunctionModelId:YWFunctionIdPostTrouble];
                if (function.offOrOn == YWFunctionStatusOn) {
                    YWPostTroubleTableViewController* postTroublevc = [[YWPostTroubleTableViewController alloc] init];
                    postTroublevc.currentEquipment = self.equipment;
                    postTroublevc.equipmentArray = self.equipment ? @[self.equipment]:nil;
                    postTroublevc.shouldPopBack = YES;
                    [self.navigationController pushViewController:postTroublevc animated:YES];
                }else{
                    UIAlertController* alertController= [UIAlertController alertControllerWithTitle:@"提示" message:@"功能已关闭。如需再次使用报障功能，您可从“设置-功能”中再次打开。" preferredStyle:UIAlertControllerStyleAlert];
                    [alertController addAction:[UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleCancel handler:nil]];
                    [self presentViewController:alertController animated:YES completion:nil];
                }
            }else if ([item.title isEqualToString:@"维修"]){
                YWFunctionModel * function = [[YWFunctionManager shareManager] getFunctionModelWithFunctionModelId:YWFunctionIdRepair];
                if (function.offOrOn == YWFunctionStatusOn) {
                    //维修（这里仅仅匹配有设备的维修任务）
                    NSArray* troubleMissions = nil;
                    if (_isScanEnter) {
                        troubleMissions = [[YWMissionManager shareManager] missionsWithQrcodeID:self.equipment.qrcodeId orLocationId:self.equipment.locationId andMaintenanceTypeId:YWMaintenanceTypeTrouble];
                    }else{
                        NSInteger value = [[[NSUserDefaults standardUserDefaults] objectForKey:KYWNoQrcodeTypeFixTroubleKey] integerValue];
                        if (value!=1) {
                            troubleMissions = [[YWMissionManager shareManager] missionsWithQrcodeID:self.equipment.qrcodeId orLocationId:self.equipment.locationId andMaintenanceTypeId:YWMaintenanceTypeTrouble];
                        }
                    }
                    if (troubleMissions.count>0) {
//                        NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"%K == %@", @"equipmentId", _equipment.equipmentId];
//                        NSArray *array = [troubleMissions filteredArrayUsingPredicate:searchPredicate];
                        NSMutableArray * array = [NSMutableArray array];
                        [troubleMissions enumerateObjectsUsingBlock:^(YWTroubleMissionModel *  _Nonnull troubleMissionModel, NSUInteger idx, BOOL * _Nonnull stop) {
                            if ([troubleMissionModel.trouble.equipment.equipmentId isEqualToString:_equipment.equipmentId]) {
                                [array addObject:troubleMissionModel];
                            }
                        }];
                        
                        if (array.count>0) {
                            troubleMissions = array;
                        }else{
                            troubleMissions = nil;
                        }
                    }
                    if (troubleMissions.count>0) {
                        YWMatchMissionsViewController *matchMissionVC = [[YWMatchMissionsViewController alloc] init];
                        matchMissionVC.matchMissions = troubleMissions;
                        matchMissionVC.qrCodeId = self.equipment.qrcodeId;
                        matchMissionVC.locationId = self.equipment.locationId;
                        [self.navigationController pushViewController:matchMissionVC animated:YES];
                    }else{
                        YWCreateTempMissionTableViewController *tempMissionVC = [[YWCreateTempMissionTableViewController alloc] init];
                        tempMissionVC.maintenanceType = YWMaintenanceTypeTrouble;
                        tempMissionVC.equipment = self.equipment;
                        tempMissionVC.qrcodeId = self.equipment.qrcodeId;
                        [self.navigationController pushViewController:tempMissionVC animated:YES];
                    }
                }else{
                    UIAlertController* alertController= [UIAlertController alertControllerWithTitle:@"提示" message:@"功能已关闭。如需再次使用维修功能，您可从“设置-功能”中再次打开。" preferredStyle:UIAlertControllerStyleAlert];
                    [alertController addAction:[UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleCancel handler:nil]];
                    [self presentViewController:alertController animated:YES completion:nil];
                }
            }else{
                //取消
            }
        }
    }];
}
*/
- (void)cellViewGestureRecognizer:(UITapGestureRecognizer*)sender{
    YWEquipmentMoreDetailTableViewController* equipmentMoreDetailVC = [YWEquipmentMoreDetailTableViewController new];
    equipmentMoreDetailVC.equipmentDataSource = self.equipmentDataSource;
    equipmentMoreDetailVC.titleDataSource = self.titleDataSource;
    equipmentMoreDetailVC.equipmentTypeId = self.equipment.equipmentTypeId;
    equipmentMoreDetailVC.equipmentId = self.equipment.equipmentId;
    [self.navigationController pushViewController:equipmentMoreDetailVC animated:YES];
}

- (void)useMethodAndRangeCellGestureRecognizer:(UITapGestureRecognizer*)sender{
    YWEquUseMethodAndRangeTableViewController *useMethodAndRangeVC = [[YWEquUseMethodAndRangeTableViewController alloc] init];
    useMethodAndRangeVC.equipmentTypeId = self.equipment.equipmentTypeId;
    [self.navigationController pushViewController:useMethodAndRangeVC animated:YES];
}

- (void)footViewGestureRecognizer:(UITapGestureRecognizer*)sender{
    if ([self.seeMoreLabel.text isEqualToString:@"查看更多报障记录"]) {
        YWEquipmentTroubleMoreTableViewController * moreTroubleVC = [[YWEquipmentTroubleMoreTableViewController alloc] init];
        moreTroubleVC.equipment = self.equipment;
        [self.navigationController pushViewController:moreTroubleVC animated:YES];
    }else if ([self.seeMoreLabel.text isEqualToString:@"查看更多巡查记录"]){
        YWInspectionEquipmentRecordMoreTableViewController * inspectionMoreVC = [[YWInspectionEquipmentRecordMoreTableViewController alloc] init];
        inspectionMoreVC.equipment = self.equipment;
        [self.navigationController pushViewController:inspectionMoreVC animated:YES];
    }else if ([self.seeMoreLabel.text isEqualToString:@"查看更多维修记录"]){
        YWEquipmentTroubleRecordMoreTableViewController * troubleMoreVC = [[YWEquipmentTroubleRecordMoreTableViewController alloc] init];
        troubleMoreVC.equipment = self.equipment;
        [self.navigationController pushViewController:troubleMoreVC animated:YES];
    }else if ([self.seeMoreLabel.text isEqualToString:@"查看更多检测记录"]){
        YWEquipmentRepairAndUnkeepMoreTableViewController * repairAndupkeepMoreVC = [YWEquipmentRepairAndUnkeepMoreTableViewController new];
        repairAndupkeepMoreVC.equipment = self.equipment;
        repairAndupkeepMoreVC.equipmnetTitle = self.seeMoreLabel.text;
        [self.navigationController pushViewController:repairAndupkeepMoreVC animated:YES];
    }else{
        YWEquipmentRepairAndUnkeepMoreTableViewController * repairAndupkeepMoreVC = [YWEquipmentRepairAndUnkeepMoreTableViewController new];
        repairAndupkeepMoreVC.equipment = self.equipment;
        repairAndupkeepMoreVC.equipmnetTitle = self.seeMoreLabel.text;
        [self.navigationController pushViewController:repairAndupkeepMoreVC animated:YES];
    }
}


//- (void)backToBefore:(UIButton*)sender{
//    [self.navigationController popViewControllerAnimated:YES];
//}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    self.addButton.hidden = NO;
    [self.navigationController.navigationBar setValue:@(0) forKeyPath:@"backgroundView.alpha"];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    self.addButton.hidden = YES;
    [self.navigationController.navigationBar setValue:@(1) forKeyPath:@"backgroundView.alpha"];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

